<?php
declare (strict_types=1);

namespace app\common\listener;

use app\common\model\Config;
use app\common\model\Relation;
use app\common\model\User;
use app\common\model\UserLevelBonus;
use think\Exception;
use think\facade\Log;

class LeaderScore
{
    /**
     * 盟主分红级别
     * @param $data
     */
    public function handle($user)
    {
        try{
            $config = Config::whereIn('name', ['rec_score_leader', 'rec_score_shoper', 'rec_score_vip'])->column('value', 'name');
            $score = [
                5 => $config['rec_score_leader'],
                4 => $config['rec_score_shoper'],
                3 => $config['rec_score_shoper'],
                2 => $config['rec_score_vip'],
            ];

            $bonus = UserLevelBonus::column('score', 'id');
            if($user->syn_level == 5){
                $this->setBonusLevel($user, $bonus);
            }
            Relation::with(['user'])->where('new_uid', $user->id)->where('algebra', '<=', 3)->field('id,uid,algebra')
                ->select()->each(function ($item) use ($score, $bonus, $user){
                    if(in_array($user->level_id, [2,3,4,5])){
                        if(isset($user->is_demotion) && $user->is_demotion == 1){
                            $item->user->rec_score -= $this->getScore($user, $score, true);//根据被邀请人的身份
                        }else{
                            $item->user->rec_score += $this->getScore($user, $score, false);//根据被邀请人的身份
                        }
                        $item->user->save();

                        if($item->user->syn_level == 5){
                            $this->setBonusLevel($item->user, $bonus);
                        }
                    }
                });
        }catch (\Exception $e){
            Log::write("listener.LeaderScore:".$e->getMessage());
            throw new Exception("加载失败：".$e->getMessage(), -1);
        }
    }

    private function setBonusLevel($user, $bonus, $level_id=10)
    {
        if($level_id > 0){
            if(isset($bonus[$level_id]) && $user->rec_score >= $bonus[$level_id]){
                $user->level_bonus_id = $level_id;
                return $user->save();
            }else{
                $level_id --;
                return $this->setBonusLevel($user, $bonus, $level_id);
            }
        }

        return false;
    }

    /**
     * 获取分数
     * @param $user
     * @param $score
     * @param false $is_demotion
     * @return mixed
     */
    private function getScore($user, $score, $is_demotion=false)
    {
        if($is_demotion){
            if(5 == $user->before_level_id){
                if(3 == $user->level_id || 4 == $user->level_id){
                    $s = $score[5];
                }elseif(2 == $user->level_id ){
                    $s = $score[3] + $score[5];
                }elseif(1 == $user->level_id){
                    $s = $score[2] + $score[3] + $score[5];
                }
            }elseif(3 == $user->before_level_id || 4 == $user->before_level_id){
                if(2 == $user->level_id){
                    $s = $score[3];
                }elseif(1 == $user->level_id){
                    $s = $score[2] + $score[3];
                }
            }elseif(2 == $user->before_level_id){
                $s = $score[2];
            }
        }else{
            if(1 == $user->before_level_id){
                if(2 == $user->level_id){
                    $s = $score[2];
                }elseif(3 == $user->level_id || 4 == $user->level_id){
                    $s = $score[2] + $score[3];
                }elseif(5 == $user->level_id){
                    $s = $score[2] + $score[3] + $score[5];
                }
            }elseif(2 == $user->before_level_id){
                if(3 == $user->level_id || 4 == $user->level_id){
                    $s = $score[3];
                }elseif(5 == $user->level_id){
                    $s = $score[3] + $score[5];
                }
            }elseif(3 == $user->before_level_id || 4 == $user->before_level_id){
                $s = $score[5];
            }
        }

        return $s;
    }

}